package com.aliang.frame.controller.admin;

import com.github.pagehelper.PageInfo;
import com.aliang.frame.base.BaseApiController;
import com.aliang.frame.base.RestResponse;
import com.aliang.frame.base.RestResponseX;
import com.aliang.frame.domain.Role;
import com.aliang.frame.domain.User;
import com.aliang.frame.domain.enums.AuthEnum;
import com.aliang.frame.domain.enums.UserStatusEnum;
import com.aliang.frame.exception.BusinessException;
import com.aliang.frame.service.RoleService;
import com.aliang.frame.utility.PageInfoHelper;
import com.aliang.frame.viewmodel.admin.role.RolePageRequestVM;
import com.aliang.frame.viewmodel.admin.role.RolePageResponseVM;
import com.aliang.frame.viewmodel.admin.role.RoleRequestVM;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.stream.Collectors;

/**
 * 角色管理
 */
@RestController("RoleController")
@RequestMapping(value = "/api/admin/role")
public class RoleController extends BaseApiController {

    private final RoleService roleService;

    public RoleController(RoleService roleService) {
        this.roleService = roleService;
    }

    /**
     * 分页获取角色列表
     * @param model
     * @return
     */
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public RestResponse<PageInfo<RolePageResponseVM>> pageList(@RequestBody RolePageRequestVM model) {
        PageInfo<Role> pageInfo = roleService.page(model);
        PageInfo<RolePageResponseVM> page = new PageInfo<>();
        page.setPageSize(pageInfo.getPageSize());
        page.setPageNum(pageInfo.getPageNum());
        page.setTotal(pageInfo.getTotal());
        page.setList(pageInfo.getList().stream().map(role -> new RolePageResponseVM(role)).collect(Collectors.toList()));
        return RestResponse.ok(page);
    }

    /**
     * 编辑角色信息
     * @param model
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public RestResponseX<RolePageResponseVM> edit(@RequestBody @Valid RoleRequestVM model) {
        if (model.getMenuIds().isEmpty()) {
            return RestResponseX.fail(505, "角色权限不能为空");
        }
        try {
            roleService.edit(model, getCurrentUser());
        } catch (BusinessException e) {
            return RestResponseX.fail(e.getCode(), e.getMessage());
        }
        RolePageResponseVM vm = roleService.roleToVM(model.getRoleId());
        return RestResponseX.ok(vm, vm.getMenuIds());
    }

    /**
     * 查询角色信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/select/{id}", method = RequestMethod.POST)
    public RestResponse<RolePageResponseVM> select(@PathVariable Integer id) {
        RolePageResponseVM vm = roleService.roleToVM(id);
        return RestResponse.ok(vm);
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    public RestResponse delete(@PathVariable Integer id) {
        Role role = roleService.selectById(id);
        if (role.getMenus().contains(String.valueOf(AuthEnum.SUPER_ADMIN.getCode()))) {
            return RestResponse.fail(505,"超级管理员无法被修改");
        }
        role.setDeleted(true);  // 标记为删除
        roleService.updateByIdFilter(role);
        return RestResponse.ok();
    }


    /**
     * 修改角色状态
     * @param id
     * @return
     */
    @RequestMapping(value = "/changeStatus/{id}", method = RequestMethod.POST)
    public RestResponse<Boolean> changeStatus(@PathVariable Integer id) {
        Role role = roleService.selectById(id);
        role.setStatus(!role.getStatus());
        role.setUpdateTime(new Date());
        role.setUpdateBy(getCurrentUser().getId());
        roleService.updateByIdFilter(role);
        return RestResponse.ok(role.getStatus());
    }
}
